package com.jsgt.greekfire.ui.coin

import android.os.Bundle
import android.view.Gravity
import android.view.View
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.viewpager.widget.ViewPager
import com.fhx.core.base.BaseActivity
import com.fhx.core.base.BaseFragment
import com.fhx.core.goToActivity
import com.fhx.core.utils.ScreenUtil
import com.fhx.core.utils.ToastUtils
import com.google.android.material.tabs.TabLayout
import com.jsgt.greekfire.R
import com.jsgt.greekfire.ui.coin.drawers.PaymentSettingsListActivity
import com.jsgt.greekfire.ui.coin.ordermanagement.OrderManagementActivity
import com.jsgt.greekfire.ui.trading.adapter.PagerAdapter
import kotlinx.android.synthetic.main.fragment_coin.*
import kotlinx.android.synthetic.main.layout_drawer_header.*

class BaseCoinFragment : BaseFragment(), View.OnClickListener {
    private val fragments: MutableList<BaseFragment> = ArrayList()
    private var coinFragment: CoinListFragment? = null

    override fun getLayoutId(): Int = R.layout.fragment_coin
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ScreenUtil.setStatusBarMode((activity as BaseActivity?)!!, true)
    }

    override fun main() {
        setTopPadding(llCoin)
        initTab()
        ivMenu.setOnClickListener(this)
        tvCoinBuy.setOnClickListener(this)
        tvCoinSell.setOnClickListener(this)
        ivOrderManagement.setOnClickListener(this)
        ivOrdersList.setOnClickListener(this)

        ivMenuOpen.setOnClickListener(this)
        rllTransactionInfo.setOnClickListener(this)
        rllAccounts.setOnClickListener(this)
        rllCustomService.setOnClickListener(this)

        val mToggle = ActionBarDrawerToggle(activity!!, drlCoin, null, 0, 0)
        drlCoin.addDrawerListener(mToggle)
        /*同步drawerlayout的状态*/
        mToggle.syncState()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ivMenu -> {
                drlCoin.openDrawer(Gravity.LEFT)
            }
            R.id.tvCoinBuy -> {
                tvCoinBuy.setTextColor(resources.getColor(R.color.color_388DEE))
                tvCoinSell.setTextColor(resources.getColor(R.color.white))
                tvCoinBuy.textSize = 26f
                tvCoinSell.textSize = 16f
            }
            R.id.tvCoinSell -> {
                tvCoinBuy.setTextColor(resources.getColor(R.color.white))
                tvCoinSell.setTextColor(resources.getColor(R.color.color_388DEE))
                tvCoinBuy.textSize = 16f
                tvCoinSell.textSize = 26f
            }
            R.id.ivOrderManagement -> {
//                ToastUtils.show(activity!!, "历史订单")
                goToActivity<OrderManagementActivity>()
            }
            R.id.ivOrdersList -> {
//                ToastUtils.show(activity!!, "订单列表")
                goToActivity<CoinsOrderActivity>()
            }
            R.id.ivMenuOpen -> {
//                ToastUtils.show(activity!!, "关")
                drlCoin.closeDrawers()
            }
            R.id.rllTransactionInfo -> {
//                ToastUtils.show(activity!!, "交易信息")
                goToActivity<TransactionInfoActivity>()
            }
            R.id.rllAccounts -> {
//                ToastUtils.show(activity!!, "法币账户")
                goToActivity<PaymentSettingsListActivity>()
            }
            R.id.rllCustomService -> {
                ToastUtils.show(activity!!, "在线客服")
            }
        }
    }


    private fun initTab() {
        var item = resources.getStringArray(R.array.coin_list)
        val tabs = java.util.ArrayList<String>()
        for (i in item.indices) {
            tabs.add(item[i])
            coinFragment = CoinListFragment.newInstance(item[i])
            fragments.add(coinFragment!!)
        }

        tabCoin.tabMode = TabLayout.MODE_FIXED
        tabCoin.setupWithViewPager(viewPager)
        viewPager.adapter = PagerAdapter(
            childFragmentManager,
            fragments,
            tabs
        )
        viewPager.currentItem = 0
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {

            }
        })
    }


}

